#! /bin/bash
#
# Get all slave source from svn
#

ECHO="/bin/echo"
PRINTF="/usr/bin/printf"
MAKE="/usr/bin/make"
MAKE_LOG_FILE="make_log.txt"
SVN="svn --non-interactive"

SLAVE_CHIP_DIR=./slave_chip
SOURCE_TOP_DIR=./

top_version=-1
svn_url=invalid_svn_url
get_master_chip_source_version()
{
	top_version=`${SVN} info ${SOURCE_TOP_DIR} 2>/dev/null | grep '^Revision'|awk '{print $NF}'`
}

get_master_chip_svn_trunk_url()
{
	svn_url=`${SVN} info ${SOURCE_TOP_DIR} 2>/dev/null | grep '^URL'|awk '{print $NF}'`
}

g_last_changed_rev=0
# $1 : folder to check
get_last_changed_rev()
{
	g_last_changed_rev=`${SVN} info $1 2>/dev/null | grep '^Last Changed Rev:'|awk '{print $NF}'`
}

# $1 : To which local folder
folder_need_update()
{
    folder_tgt=$1
    #folder_src=`${ECHO} $1| sed -e 's/${SLAVE_CHIP_DIR}//'`
    folder_src=${1/${SLAVE_CHIP_DIR}/.}
    get_last_changed_rev ${folder_tgt}
    last_changed_rev_tgt=${g_last_changed_rev}
    get_last_changed_rev ${folder_src}
    last_changed_rev_src=${g_last_changed_rev}

    ${ECHO} "Compare ${folder_tgt}@${last_changed_rev_tgt} ${folder_src}@${last_changed_rev_src}"
    if [[ ${last_changed_rev_src} == ${last_changed_rev_tgt} ]]
    then
        ${ECHO} "no need to udpate"
        return 0
    fi
    return 1
}

# $1 : From which svn URL
# $2 : To which local folder
get_source_code_by_path()
{
	to_folder=$2
	from_url=$1
	${ECHO} "Get source code from" $1 "to " $2
	if [[ -d ${to_folder} ]]
	then
	    ${ECHO} "Folder exist, see if need to update"
        folder_need_update $2 
        if [[ $? == 1 ]]
        then
            ${SVN} update $2 -r ${top_version}
        fi
	else
	    ${ECHO} "Folder not exist, checkout "
	    ${SVN} co $1 $2 -r ${top_version}
	fi
}

get_master_chip_source_version
get_master_chip_svn_trunk_url
${ECHO} "Master chip software URL" ${svn_url}
${ECHO} "Master chip software version" ${top_version}
# Get scripts need by build evnvironment
get_source_code_by_path ${svn_url}/scripts                  ${SLAVE_CHIP_DIR}/scripts
# btools(himm.d/l) and udev(udevd/info)
get_source_code_by_path ${svn_url}/app/udev                 ${SLAVE_CHIP_DIR}/app/udev
get_source_code_by_path ${svn_url}/app/reg-tools            ${SLAVE_CHIP_DIR}/app/reg-tools
get_source_code_by_path ${svn_url}/app/handshake            ${SLAVE_CHIP_DIR}/app/handshake
# gv7601 and ssp driver
get_source_code_by_path ${svn_url}/exdrv/cx259xx            ${SLAVE_CHIP_DIR}/exdrv/cx259xx
get_source_code_by_path ${svn_url}/exdrv/gv7601             ${SLAVE_CHIP_DIR}/exdrv/gv7601
get_source_code_by_path ${svn_url}/exdrv/ssp                ${SLAVE_CHIP_DIR}/exdrv/ssp
get_source_code_by_path ${svn_url}/exdrv/hios_mcc_usrdev    ${SLAVE_CHIP_DIR}/exdrv/hios_mcc_usrdev
get_source_code_by_path ${svn_url}/exdrv/pca955             ${SLAVE_CHIP_DIR}/exdrv/pca955
get_source_code_by_path ${svn_url}/exdrv/i2c_module         ${SLAVE_CHIP_DIR}/exdrv/i2c_module
get_source_code_by_path ${svn_url}/exdrv/inc                ${SLAVE_CHIP_DIR}/exdrv/inc

# uboot and busybox
get_source_code_by_path ${svn_url}/firmware/uboot/          ${SLAVE_CHIP_DIR}/firmware/uboot/
get_source_code_by_path ${svn_url}/firmware/busybox/        ${SLAVE_CHIP_DIR}/firmware/busybox/
# kernel
get_source_code_by_path ${svn_url}/firmware/kernel/         ${SLAVE_CHIP_DIR}/firmware/kernel/

#header files
get_source_code_by_path ${svn_url}/inc/                     ${SLAVE_CHIP_DIR}/inc
#lib needed
get_source_code_by_path ${svn_url}/comp/video/              ${SLAVE_CHIP_DIR}/comp/video
get_source_code_by_path ${svn_url}/comp/audio/              ${SLAVE_CHIP_DIR}/comp/audio
get_source_code_by_path ${svn_url}/comp/pciv/               ${SLAVE_CHIP_DIR}/comp/pciv/
get_source_code_by_path ${svn_url}/comp/module/devlib       ${SLAVE_CHIP_DIR}/comp/module/devlib
get_source_code_by_path ${svn_url}/comp/base/common         ${SLAVE_CHIP_DIR}/comp/base/common
get_source_code_by_path ${svn_url}/comp/base/mshead         ${SLAVE_CHIP_DIR}/comp/base/mshead
get_source_code_by_path ${svn_url}/comp/base/mem_manage     ${SLAVE_CHIP_DIR}/comp/base/mem_manage
get_source_code_by_path ${svn_url}/comp/base/_include       ${SLAVE_CHIP_DIR}/comp/base/_include
get_source_code_by_path ${svn_url}/comp/base/ushell         ${SLAVE_CHIP_DIR}/comp/base/ushell

get_source_code_by_path ${svn_url}/tool/sig_tracer          ${SLAVE_CHIP_DIR}/tool/sig_tracer

# vim:et:sw=4:ts=4

